<script>
$(function() {
	$('input.passenger_name').autocomplete({
		source: base_url + 'index.php/frontend/booking/get_customer_passenger_json',
		select: function(event, ui) {
			$ptr = $(this).parent().parent('tr');
			$ptr.find('input.passenger_id').val(ui.item.id);
			$ptr.find('select.sel_certification_type').val(ui.item.certification_type);
			$ptr.find('input.certification_number').val(ui.item.certification_number);
		}
	}).each(function() {
		$(this).data('autocomplete')._renderItem = function(ul, item) {
	        return $('<li>')
	        .data('item.autocomplete', item)
	        .append(
				'<a>' + 
				'<div class="text-success">Passenger Name: <strong>' + item.passenger_name + '</strong></div>' +
				'<div class="text-info" id="' + item.certification_type + '">Certification Type: ' + ((item.certification_type == 1) ? 'Identification Card' : (item.certification_type == 2) ? 'Officer Certificate' : '-') + '</div>' +
				'<div class="text-info">Certification Number: ' + ((item.certification_number) ? item.certification_number : '-') + '</div>' +
				'</a>'
			)
	        .appendTo(ul);
		};
	});
});
</script>
<div class="well">
	<table class="table table-bordered" id="selected_itinerary_block">
		<caption><strong>Selected Itinerary</strong></caption>
		<thead>
			<tr>
				<th>Flight</th>
				<th>Departure</th>
				<th>Arrival</th>
				<th>Class/Status</th>
			</tr>
		</thead>
		<tbody>
			<?php foreach ($selected_itinerary as $k => $v):?>
			<tr <?php if ($k % 2 == 0):?> class="info" <?php endif;?>>
				<td><?php echo $v['scheduler']['flight_code'];?>
				</td>
				<td>
				<?php echo $v['airport']['from']['airport_name'] . (($v['airport']['from']['airport_name'] != $v['airport']['from']['area']['name']) ? ('/' . $v['airport']['from']['area']['name']) : '');?>
				<br />
				<?php echo date('d M (D) H:i', strtotime($v['departure_date'] . ' ' . $v['scheduler']['departure_time']));?>
				</td>
				<td>
				<?php echo $v['airport']['to']['airport_name'] . (($v['airport']['to']['airport_name'] != $v['airport']['to']['area']['name']) ? ('/' . $v['airport']['to']['area']['name']) : '');?>
				<br />
				<?php echo date('d M (D) H:i', strtotime($v['departure_date'] . ' ' . $v['scheduler']['arrival_time']));?>
				</td>
				<td>
				<?php echo ($booking_data['cabin_class'] == 'Y') ? 'Economy' : 'Prestige';?>
				<br />
				<?php echo ($booking_data['adult_count'] + $booking_data['child_count']) . ' Seat';?>
				</td>
			</tr>
			<?php endforeach;?>
		</tbody>
	</table>
	<table class="table table-bordered" id="passenger_information_block">
		<caption><strong>Passenger Information</strong></caption>
		<thead>
			<tr>
				<th style="width: 40px;">Passenger Name</th>
				<th style="width: 95px;">Certification Type</th>
				<th style="width: 40px;">Certification Number</th>
				<th style="width: 40px;">Individual Discount</th>
			</tr>
		</thead>
		<tbody>
			<?php for ($i = 0; $i < ($booking_data['adult_count'] + $booking_data['child_count'] + $booking_data['infant_count']); $i++):?>
			<?php
			if ($i + 1 <= $booking_data['adult_count']) { 
				$passenger_type = 'adult';
			} elseif ($i + 1 > $booking_data['adult_count'] && $i + 1 <= $booking_data['adult_count'] + $booking_data['child_count']) {
				$passenger_type = 'child';
			} else {
				$passenger_type = 'infant';
			}
			?>
			<tr class="<?php echo $passenger_type;?> <?php if ($i % 2 == 0):?>info<?php endif;?>">
				<input type="hidden" class="passenger_id" name="passenger_id_<?php echo $i;?>">
				<td><input type="text" class="span12 passenger_name"></td>
				<td>
					<?php if ($passenger_type == 'adult'):?>
					<select class="span11 sel_certification_type" >
						<option value="">---Select Type---</option>
						<option value="1">Identification Card</option>
						<option value="2">Officer Certificate</option>
					</select>
					<?php elseif ($passenger_type == 'child'):?>
					Identification Card
					<?php else:?>
					-
					<?php endif;?>
				</td>
				<td>
					<?php if ($passenger_type == 'adult' || $passenger_type == 'child'):?>
					<input type="text" class="span12 certification_number">
					<?php else:?>
					-
					<?php endif;?>
				</td>
				<td>
				<?php if ($passenger_type == 'adult'):?>
				<abbr title="Above 13 Years" class="initialism">Adult</abbr><br />No Discount
				<?php elseif ($passenger_type == 'child'):?>
				<abbr title="Age 2 ~ 12 years" class="initialism">Child</abbr><br />25% Discount
				<?php else:?>
				<abbr title="Under 2 years" class="initialism">Infant</abbr><br />100% Discount
				<?php endif;?>
				</td>
			</tr>
			<?php endfor;?>
		</tbody>
	</table>
	<button class="btn btn-mini btn-success" type="button" id="next">Next</button>
</div>